package com.alex.mvppractice;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.alex.mvppractice.about.AboutFragment;
import com.alex.mvppractice.constant.UrlString;
import com.alex.mvppractice.image.ImageFragment;
import com.alex.mvppractice.main.MainView;
import com.alex.mvppractice.main.presenter.MainPresenter;
import com.alex.mvppractice.news.widget.NewsFragment;
import com.alex.mvppractice.weather.WeatherFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity implements MainView {

    @InjectView(R.id.navigation_view)
    NavigationView mNavigationView;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.appbar)
    AppBarLayout mAppbar;
    @InjectView(R.id.frame_content)
    FrameLayout mFrameContent;
    @InjectView(R.id.main_content)
    CoordinatorLayout mMainContent;
    @InjectView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;

    private MainPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        System.out.println(String.format(UrlString.url_detail, "123lllll"));

        String format = String.format("mszs,%s,%d", "你说的对！", 10);
        System.out.println(format);

        setSupportActionBar(mToolbar);
//        mToolbar.setNavigationIcon(R.mipmap.ic_launcher);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
                R.string.drawer_open, R.string.drawer_close);
        toggle.syncState();
        mDrawerLayout.addDrawerListener(toggle);

        mPresenter = new MainPresenter(this);

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int itemId = item.getItemId();

                mPresenter.switchFragment(itemId);

                mNavigationView.setCheckedItem(itemId);
                mDrawerLayout.closeDrawers();
                return false;
            }
        });

        toNewFragment();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void toNewFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new NewsFragment()).commit();
        mToolbar.setTitle(R.string.navigation_news);
    }

    @Override
    public void toImageFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new ImageFragment()).commit();
        mToolbar.setTitle(R.string.navigation_images);
    }

    @Override
    public void toWeatherFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new WeatherFragment()).commit();
        mToolbar.setTitle(R.string.navigation_weather);
    }

    @Override
    public void toAboutFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new AboutFragment()).commit();
        mToolbar.setTitle(R.string.navigation_about);
    }

    //NavigationView的使用介绍
//    private void navigation() {
//        View headerView = mNavigationView.getHeaderView(0);
//        CircleImageView iv = (CircleImageView) headerView.findViewById(R.id.profile_image);
//        iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "点击了头部。。。", Toast.LENGTH_SHORT).show();
//            }
//        });
//
//        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
//            @Override
//            public boolean onNavigationItemSelected(MenuItem item) {
//                int itemId = item.getItemId();
//                switch (itemId) {
//                    case R.id.navigation_item_news:
//                        Toast.makeText(MainActivity.this, "点击了新闻", Toast.LENGTH_SHORT).show();
//                        break;
//                    case R.id.navigation_item_images:
//                        Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
//                        break;
//                }
//
//                mNavigationView.setCheckedItem(itemId);
//                mDrawerLayout.closeDrawers();
//                return false;
//            }
//        });
//    }
}
